package com.example.demo.controller;

import com.example.demo.entity.Message;
import com.example.demo.exception.BizException;
import com.example.demo.service.MessageService;
import com.example.demo.util.EmptyUtil;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/message/")
public class MessageController {
    private static final Logger log = LoggerFactory.getLogger(MessageController.class);

    @Autowired
    private MessageService messageService;

    /**
     * 查询消息列表
     * @return
     */
    @ApiOperation(value = "查询未读消息列表", notes = "数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name="userId",value = "用户编号",required = false, paramType = "query"),
            @ApiImplicitParam(name="status",value = "消息状态",required = false, paramType = "query")
    })
    @RequestMapping(value = "getMessageList",method = RequestMethod.GET)
    public List<Message> getMessageList(@RequestBody Message message){
        return messageService.getMessageList(message);
    }

    /**
     * 新增消息
     * @param
     * @return
     */
    @ApiOperation(value = "新增消息", notes = "数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name="userId",value = "用户编号",required = true, paramType = "query"),
            @ApiImplicitParam(name="content",value = "消息内容",required = true, paramType = "query")
    })
    @RequestMapping(value = "addMessage",method = RequestMethod.GET)
    public List<Message> addMessage(Message message){
        if(EmptyUtil.isEmpty(message)){
            throw new BizException();
        }
        try {
            messageService.addMessage(message);
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
        return messageService.getMessageList(null);
    }

    /**
     * 修改消息
     * @param
     * @return
     */
    @RequestMapping(value = "updateMessage",method = RequestMethod.GET)
    public List<Message> updateMessage(Message message){
        if(null == message || null == message.getId()){
            return null;
        }
        try {
            Message queryMessage = new Message();
            queryMessage = messageService.queryMessage(message);
            if(queryMessage != null ) {
                messageService.updateMessage(message);
            }else{
                return null;
            }
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
        return messageService.getMessageList(null);
    }
}
